package com.stnet.service.enterprice.chinamobile;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import com.modules.commons.Pager;
import com.modules.util.DateHelper;
import com.modules.util.MD5Util;
import com.stnet.bases.BaseManagerDaoImpl;
import com.stnet.bases.ReadProperties;
import com.stnet.entity.OnlineRecharge;
import com.stnet.entity.Users;

public class OnlineRechargeManagerDaoImpl extends BaseManagerDaoImpl implements OnlineRechargeManagerDao {
	
	
	private String out_trade_id;

	public void addOnlineRecharge(OnlineRecharge onlineRecharge) {
		onlineRechargeDao.save(onlineRecharge);
	}
	
	public OnlineRecharge getByOuttradeid(String outtradeid) {
		return onlineRechargeDao.getByOuttradeid(outtradeid);
	}

	public void deleteOnlineRecharge(Long id) {
		onlineRechargeDao.delete(id);
	}

	public Pager findOnlineRecharge(int i, int j, String s) {
		Pager pager = onlineRechargeDao.getObjectListByClass(i, j, OnlineRecharge.class, s);
		return pager;
	}

	public List findOnlineRechargeByAgent(Long agent) {
		return onlineRechargeDao.findOnlineRechargeByAgent(agent);
	}

	public Pager getListByAgent(int pageSize, int pageNo, Long agent) {
		return onlineRechargeDao.getListByAgent(pageSize, pageNo, agent);
	}

	public OnlineRecharge getOnlineRecharge(Long id) {
		return onlineRechargeDao.get(id);
	}
	
	public void updateOnlineRecharge(OnlineRecharge onlinerecharge) {
		onlineRechargeDao.update(onlinerecharge);
	}

	public int onlineRecharge(String mobile, int value, int type, Users users) {
		ReadProperties rp = new ReadProperties("onlinerecharge.properties");
		Properties p = rp.getProperties();
		String PARTNER = p.getProperty("customer.id");// 代理商id
		String KEY = p.getProperty("customer.key");
		String MOBILE_URL = p.getProperty("directurl");
		String NOTIFY_URL = p.getProperty("notifyurl");
		String CHARSET = "utf-8";
		out_trade_id = new DateHelper().getRandomNum();//代理商交易序号（唯一）
		int result = 0;
		StringBuilder sb = new StringBuilder();
		StringBuilder sb1 = new StringBuilder();
		StringBuilder sb2 = new StringBuilder();
		sb.append("partner=").append(PARTNER)
		.append("&out_trade_id=").append(out_trade_id)
		.append("&mobile=").append(mobile)
		.append("&value=").append(value)
		.append("&type=").append(type)
		.append("&notify_url=").append(NOTIFY_URL);
		sb1.append(sb.toString()).append("&").append(KEY);
		String sign = MD5Util.MD5(sb1.toString());
		
		sb2.append(sb.toString())
		.append("&sign=").append(sign)
		.append("&charset=").append(CHARSET);
		String param = sb2.toString();
		
		String sCurrentLine;
		String sTotalString;
		sCurrentLine = "";
		sTotalString = "";
		InputStream l_urlStream;
		try {
			URL url = new URL(MOBILE_URL);
			URLConnection connection = url.openConnection();
			connection.setDoOutput(true);
			OutputStreamWriter out = new OutputStreamWriter(connection
					.getOutputStream(), "utf-8");
			out.write(param); // post的关键所在！
			out.flush();
			out.close();
			
			l_urlStream = connection.getInputStream();
			// 传说中的三层包装阿！
			BufferedReader l_reader = new BufferedReader(new InputStreamReader(
					l_urlStream, "utf-8"));
			while ((sCurrentLine = l_reader.readLine()) != null) {
				sTotalString += sCurrentLine;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		int i = sTotalString.indexOf("<result>");
		String strResult = sTotalString.substring(i+8,i+9);
		result = Integer.valueOf(strResult);
		
		try {
			OnlineRecharge onlineRecharge = new OnlineRecharge();
			onlineRecharge.setPartner(PARTNER);
			onlineRecharge.setResult(result);
			onlineRecharge.setOuttradeid(out_trade_id);
			onlineRecharge.setAccount(mobile);
			onlineRecharge.setEditetime(new Date());
			onlineRecharge.setUser(users);
			onlineRecharge.setAmount((long)value);
			onlineRechargeDao.save(onlineRecharge);
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		
		return result;
	}


}
